<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	手机站页面组件
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'installed';
$tabs=array(
	array('op'=>'installed','title'=>'已安装的组件'),
	array('op'=>'updates','title'=>'待更新的组件'),
	array('op'=>'prepared','title'=>'已购买待安装的组件'),
	//array('op'=>'update_more','title'=>'一键同步组件代码'),
);
$return=array();
//exi('cs','error');
if($_RQ['op'] == 'installed') {
	//已安装组件列表，此处显示所有安装的组件，但是不检测更新
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','release','cloud_id','showhtml','edithtml');
	$list=pdo_getslice('core_mobile_blocks',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$showhtml=ROOT_AT.$item['showhtml'];
		$edithtml=ROOT_AT.$item['edithtml'];
		if($item['cloud_id'] && (!is_file($showhtml) || !is_file($edithtml))){
			$item['nodata']=1;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'管理已安装的手机端页面组件,如果显示《需同步》，说明该组件的页面附件文件缺失：缺失原因很多，比如附件保存地址变更了，但是附件文件没有对应复制过去！或者搬站的时候没有把附件搬过去！','data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'title','title'=>'组件名称','type'=>'string'),
			array('name'=>'release','title'=>'更新时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'uninstall','title'=>'卸载'),
			array('type'=>'post','op'=>'update_moredo','title'=>'需同步','show'=>array('nodata'=>1)),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if ($_RQ['op'] == 'uninstall') {
	//卸载模版。卸载模版时，服务套餐中有包含该模版的，公众号有购买该模版的，是否同时将相关数据删除？暂不删除！
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择组件！','error');
	}
	pdo_delete('core_mobile_blocks',array('id'=>$id));
	exi('卸载成功！');
}
if($_RQ['op'] == 'updates') {
	//待更新组件列表，此处显示所有待更新的组件
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fields=array('id','title','release','cloud_id');
	$list=pdo_getall('core_mobile_blocks',$params,$fields,'cloud_id');
	$cloud_ids=';';
	foreach($list as &$item){
		$cloud_ids.=$item['cloud_id'].';';
	}
	if($cloud_ids!=';'){
		$cloud=cloud_request('update.mobile_blocks.check',array('ids'=>$cloud_ids));
		foreach($cloud as $key=>$row){
			if($list[$key]['release']<$row['release']){
				//存在新版本
				$list[$key]['release1']=$row['release'];
			}else{
				unset($list[$key]);
			}
		}
	}
	$return['list']=$list?$list:array();
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'管理已安装的页面组件','data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'pagelist'=>false,'list'=>array(
			array('name'=>'title','title'=>'组件名称','type'=>'string'),
			array('name'=>'release','title'=>'当前版本','type'=>'time'),
			array('name'=>'release1','title'=>'最新版本','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'update','title'=>'更新'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op'] == 'update'){
	//更新页面组件
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择组件！','error');
	}
	$block=pdo_get('core_mobile_blocks',array('id'=>$id),array('cloud_id','showhtml','edithtml'));
	if(!$block){
		exi('指定的组件不存在！','error');
	}
	$data=cloud_request('update.mobile_blocks.update',array('id'=>$block['cloud_id']));
	$data['showhtml']=cfc('files')->source_save($data['showhtml'],'page','html','',$block['showhtml']);
	$data['edithtml']=cfc('files')->source_save($data['edithtml'],'page','html','',$block['edithtml']);
	pdo_update('core_mobile_blocks',$data,array('id'=>$id));
	exi('更新成功！');
}
if($_RQ['op'] == 'update_more'){
	//更新不存在的页面组件
	$list=pdo_getall('core_mobile_blocks',array(),array('id','showhtml','edithtml','cloud_id'),'cloud_id');
	$needup=array();
	foreach($list as $item){
		$showhtml=ROOT_AT.$item['showhtml'];
		$edithtml=ROOT_AT.$item['edithtml'];
		if($item['cloud_id'] && (!is_file($showhtml) || !is_file($edithtml))){
			$needup[]=$item;
		}
	}
	$_SESSION['needup']=$needup;
	exi('已整理好，开始更新！','','core/cloud.mobile_blocks/update_moredo/id='.$_SESSION['needup'][0]['id']);
}
if($_RQ['op'] == 'update_moredo'){
	//更新页面组件
	$id=intval($_RQ['id']);
	$key=intval($_RQ['key']);
	if(!$id){
		exi('请选择组件！','error');
	}
	$block=pdo_get('core_mobile_blocks',array('id'=>$id),array('cloud_id','showhtml','edithtml'));
	if(!$block){
		exi('指定的组件不存在！','error');
	}
	$data=cloud_request('update.mobile_blocks.update',array('id'=>$block['cloud_id']));
	$data['showhtml']=cfc('files')->source_save($data['showhtml'],'page','html','H5页面组件：'.$item['title'].'文件');
	$data['edithtml']=cfc('files')->source_save($data['edithtml'],'page','html','H5页面组件：'.$item['title'].'文件');
	pdo_update('core_mobile_blocks',$data,array('id'=>$id));
	exi('更新成功！');
}
if($_RQ['op'] == 'prepared'){
	//待安装组件列表
	$installed = pdo_getall('core_mobile_blocks',array(),array('cloud_id'),'cloud_id');
	if($installed){
		$installed=array_keys($installed);
		$installed=implode(';',$installed);
	}else{
		$installed='';
	}
	$result=cloud_request('update.mobile_blocks.getbuyed',array('installed'=>$installed));
	$return['list']=$result;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'此处显示已购买但尚未安装的pc端页面组件。如尚未购买组件，可前往应用商城（http://s.jinyunweb.com/manage/index.php?plugin=core&action=store.mobile_block）购买。还可以在【系统管理——云服务——框架在线更新——自动购买官方免费应用】一键购买免费应用！','data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'pagelist'=>false,'list'=>array(
			array('name'=>'title','title'=>'组件名称','type'=>'string'),
			array('name'=>'release','title'=>'组件最新发布日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'install','title'=>'安装'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op'] == 'install') {
	//安装组件
	$id=trim($_RQ['id']);
	if(!$id){
		exi('请先选择要安装的组件！','error');
	}
	$check=pdo_getcolumn('core_mobile_blocks',array('cloud_id'=>$id),'id');
	if($check){
		exi('该组件已安装，无需重复安装！','error');
	}
	$item=cloud_request('update.mobile_blocks.install',array('id'=>$id));
	$item['showhtml']=cfc('files')->source_save($item['showhtml'],'page','html','H5页面组件：'.$item['title'].'文件');
	$item['edithtml']=cfc('files')->source_save($item['edithtml'],'page','html','h5页面组件：'.$item['title'].'文件');
	$item['cloud_id']=$id;
	pdo_insert('core_mobile_blocks',$item);
	exi('安装成功！');
}
